﻿using NPOI.SS.UserModel;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UIWindow.Entitys;
using UIWindow.GlobalParameters;

namespace UIWindow.Services
{
    public class DataQueryService
    {
        private ParameterGlobal _parameter;
        public DataQueryService(ParameterGlobal parameter)
        {
            _parameter = parameter;
        }

        /// <summary>
        /// 查询数据
        /// </summary>
        /// <param name="startTime">开始时间</param>
        /// <param name="endTime">结束时间</param>
        /// <param name="productModel">批次号</param>
        /// <param name="taryNumber">条码</param>
        /// <param name="employeeID">员工ID</param>
        /// <returns></returns>
        public async Task<IList<ProductionData>> GetData(DateTime startTime, DateTime endTime, string productModel, string taryNumber, string employeeID, string str)
        {
            return await Task.Run(() =>
            {
                ProductionData.Meta.TableName = str;
                XCode.Expression expression = ProductionData._.FrontScanTime >= startTime & ProductionData._.FrontScanTime <= endTime;
                if (!string.IsNullOrEmpty(productModel)) expression &= ProductionData._.ProductBatch == productModel;
                if (!string.IsNullOrEmpty(taryNumber)) expression &= ProductionData._.BarCode.Contains(taryNumber);
                if (!string.IsNullOrEmpty(employeeID)) expression &= ProductionData._.UserName == employeeID;
                return ProductionData.FindAll(expression);
            });
        }

        /// <summary>
        /// 导出数据
        /// </summary>
        /// <param name="path">路径</param>
        /// <param name="datas">数据集</param>
        /// <returns></returns>
        public async Task ExportData(string path, IList<ProductionData> list)
        {
            await Task.Run(() =>
            {
                string importExcelPath = "./Export template.xls";
                var fields = ProductionData.Meta.AllFields;
                IWorkbook workbook = WorkbookFactory.Create(importExcelPath);
                ISheet sheet = workbook.GetSheet("Injection data");//获取第一个工作薄
                int count = 0;
                int countSender = 0;
                IRow row = sheet.CreateRow(count);
                for (int i = 0; i < fields.Length; i++) row.CreateCell(i, CellType.String);
                var list2 = new ProductionData().GetType().GetProperties();
                foreach (var sender in list2)
                {
                    if (sender.Module.Name != "UIWindow.exe" && sender.Name != "Items" && sender.Name != "Item" && sender.Name != "ID" && sender.Name != "Extends")
                    {
                        var model = fields.FirstOrDefault(x => x.ColumnName == sender.Name);
                        row.Cells[countSender].SetCellValue(model.Description);
                        countSender += 1;
                    }
                }
                count += 1;
                foreach (var item in list)
                {
                    row = sheet.CreateRow(count);
                    for (int i = 0; i < fields.Length; i++) row.CreateCell(i, CellType.String);
                    countSender = 0;
                    var list1 = item.GetType().GetProperties();
                    foreach (var sender in list1)
                    {
                        if (sender.Module.Name != "UIWindow.exe" && sender.Name != "Items" && sender.Name != "Item" && sender.Name != "ID" && sender.Name != "Extends")
                        {
                            if (_parameter.RunParameter.ProductType == "二次注液")
                            {
                                if (sender.Name != "一注前称重" && sender.Name != "一注后称重")
                                {
                                    row.Cells[countSender].SetCellValue($"{sender.GetValue(item, null)}");
                                    countSender += 1;
                                }
                            }
                            else
                            {
                                row.Cells[countSender].SetCellValue($"{sender.GetValue(item, null)}");
                                countSender += 1;
                            }
                        }
                    }
                    count += 1;
                }
                //导出excel
                FileStream fs = new FileStream($"{path}\\注液机表格{DateTime.Now:yyyyMMddHH}.xls", FileMode.Create, FileAccess.Write);
                workbook.Write(fs);
                fs.Close();
            });
        }
    }
}
